package com.google.code.mochaccino.framework.ui.demo;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.freemarker.TemplateManager;
import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.ui.components.Option;
import com.google.code.mochaccino.framework.ui.components.table.Cell;
import com.google.code.mochaccino.framework.ui.components.table.Column;
import com.google.code.mochaccino.framework.ui.components.table.Row;
import com.google.code.mochaccino.framework.ui.freemarker.TemplateOutput;
import com.google.code.mochaccino.framework.ui.i18n.ResourceBundleUtil;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.CollectionModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

/**
 *
 */
@Path( "/demo" )
public class Demo {
	private TemplateManager manager;

	@Inject
	public Demo( TemplateManager mgr ) {
		this.manager = mgr;
	}

	@Path( "/button.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response button() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "subtemplatePath", "/web/demo/button.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/datepicker.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response datepicker() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/widgets/ui-picker", Locale.ENGLISH ) );
		param.put( "subtemplatePath", "/web/demo/datepicker.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/datetimepicker.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response datetimepicker() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/widgets/ui-picker", Locale.ENGLISH ) );
		param.put( "subtemplatePath", "/web/demo/datetimepicker.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/dialogs.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response dialog_message() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "subtemplatePath", "/web/demo/dialogs.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/list-pane.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response list_pane() {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Column> columns = new ArrayList<Column>();

		Column col1 = new Column( "C1", "C1", 125, true );
		col1.setSortable( true );
		col1.setSortDirection( Column.SORT_ASC );
		columns.add( col1 );

		Column col2 = new Column( "C2", "C2", 125, true );
		col2.setSortable( true );
		columns.add( col2 );

		Column col3 = new Column( "C3", "C3", 125, false );
		col3.setSortable( true );
		columns.add( col3 );

		Column col4 = new Column( "C4", "C4", 125, true );
		col4.setSortable( true );
		columns.add( col4 );

		List<Row> rows = new ArrayList<Row>();
		for ( int i = 0; i < 5; i++ ) {
			Row row = new Row( "R" + i, false, null );
			row.addCell( col1.createCell( "ROW_" + i + "_COL1" ) );
			row.addCell( col2.createCell( "ROW_" + i + "_COL2" ) );
			row.addCell( col3.createCell( "ROW_" + i + "_COL3" ) );
			row.addCell( col4.createCell( "ROW_" + i + "_COL4" ) );
			rows.add( row );
		}

		for ( int i = 5; i < 10; i++ ) {
			Row row = new Row( "R" + i, false, null );
			row.addCell( col1.createCell( "ROW_" + i + "_COL1" ) );

			Cell cell2 = col2.createCell( "/img/calendar.png" );
			cell2.setDataIsImage( true );
			cell2.setHref( "http://www.apple.ca/" );
			row.addCell( cell2 );

			Cell cell3 = col3.createCell( "/img/calendar.png" );
			cell3.setDataIsImage( true );
			row.addCell( cell3 );

			Cell cell4 = col4.createCell( "ROW_" + i + "_COL4" );
			cell4.setHref( "http://www.google.ca/" );

			row.addCell( cell4 );
			rows.add( row );
		}

		param.put( "columns", new CollectionModel( columns, BeansWrapper.getDefaultInstance() ) );
		param.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/components/ui-list-pane", Locale.ENGLISH ) );
		param.put( "rows", new CollectionModel( rows, BeansWrapper.getDefaultInstance() ) );
		param.put( "subtemplatePath", "/web/demo/list_pane.html.ftl" );
		return Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/livemode.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response livemode() {
		Map<String,Object> param = new HashMap<String,Object>();
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo-live-mode.html.ftl", param ) ).build();
	}

	@Path( "/notification.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response notification() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "subtemplatePath", "/web/demo/notification.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/select.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response select() {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Option> optList = new ArrayList<Option>();
		optList.add( new Option( "Test1", "Test1", false ) );
		optList.add( new Option( "Test2", "Test2", false ) );
		param.put( "choices", new CollectionModel( optList, BeansWrapper.getDefaultInstance() ) );
		param.put( "subtemplatePath", "/web/demo/select.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/select-chosen.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response selectchosen() {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Option> optList = new ArrayList<Option>();
		optList.add( new Option( "Test1", "Test1", false ) );
		optList.add( new Option( "Test2", "Test2", false ) );
		param.put( "choices", new CollectionModel( optList, BeansWrapper.getDefaultInstance() ) );
		param.put( "subtemplatePath", "/web/demo/select-chosen.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/select-chosen-multi.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response selectchosen_multi() {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Option> optList = new ArrayList<Option>();
		optList.add( new Option( "Test1", "Test1", false ) );
		optList.add( new Option( "Test2", "Test2", false ) );
		optList.add( new Option( "Test3", "Test3", false ) );
		param.put( "choices", new CollectionModel( optList, BeansWrapper.getDefaultInstance() ) );
		param.put( "subtemplatePath", "/web/demo/select-chosen-multi.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/demo.less" )
	@GET
	@Produces( MimeTypes.TYPE_CSS )
	public Response style() {
		Map<String,Object> param = new HashMap<String,Object>();
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.less", param ) ).build();
	}

	@Path( "/table.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response table() {
		Map<String,Object> param = new HashMap<String,Object>();
		List<Column> columns = new ArrayList<Column>();

		Column col1 = new Column( "C1", "C1", 125, true );
		col1.setSortable( true );
		columns.add( col1 );

		Column col2 = new Column( "C2", "C2", 125, true );
		col2.setSortable( true );
		columns.add( col2 );

		Column col3 = new Column( "C3", "C3", 125, false );
		col3.setSortable( true );
		columns.add( col3 );

		Column col4 = new Column( "C4", "C4", 125, true );
		col4.setSortable( false );
		columns.add( col4 );

		List<Row> rows = new ArrayList<Row>();
		for ( int i = 0; i < 10; i++ ) {
			Row row = new Row( "R" + i, false, null );
			row.addCell( col1.createCell( "ROW_" + i + "_COL1" ) );
			row.addCell( col2.createCell( "ROW_" + i + "_COL2" ) );
			row.addCell( col3.createCell( "ROW_" + i + "_COL3" ) );
			row.addCell( col4.createCell( "ROW_" + i + "_COL4" ) );
			rows.add( row );
		}

		param.put( "columns", new CollectionModel( columns, BeansWrapper.getDefaultInstance() ) );
		param.put( "rows", new CollectionModel( rows, BeansWrapper.getDefaultInstance() ) );
		param.put( "subtemplatePath", "/web/demo/table.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/textarea.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response textarea() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "subtemplatePath", "/web/demo/textarea.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/textfield.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response textfield() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "subtemplatePath", "/web/demo/textfield.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

	@Path( "/timepicker.html" )
	@GET
	@Produces( MimeTypes.TYPE_HTML )
	public Response timepicker() {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/widgets/ui-picker", Locale.ENGLISH ) );
		param.put( "subtemplatePath", "/web/demo/timepicker.html.ftl" );
		return (Response) Response.ok().entity( new TemplateOutput( manager, "/web/demo/demo.html.ftl", param ) ).build();
	}

}
